//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.TridentWordAddIn.Common;

namespace Microsoft.TridentWordAddIn.Presentation
{
    internal partial class WebServiceStoreInformation : UserControl
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="WebServiceStoreInformation"/> class.
        /// </summary>
        public WebServiceStoreInformation()
        {
            InitializeComponent();
        }

        #endregion

        /// <summary>
        /// Occurs when [on validation].
        /// </summary>
        public event EventHandler<ConnectionValidateEventArgs> OnValidation;

        #region Properties

        /// <summary>
        /// Gets or sets the bucket.
        /// </summary>
        /// <value>The bucket.</value>
        public string Bucket
        {
            get
            {
                return this.cmbBucket.Text;
            }
            set
            {
                this.cmbBucket.Text = value;
            }
        }

        #endregion

        #region Events

        /// <summary>
        /// Handles the Validating event of the cmbBucket control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
        private void cmbBucket_Validating(object sender, CancelEventArgs e)
        {
            if (this.OnValidation != null)
            {
                ValidationResult result = this.IsValidBucket();
                ConnectionValidateEventArgs connectionValidateEventArgs = new ConnectionValidateEventArgs(result.GetMessage(), result.IsValid, "ComboBox");
                this.OnValidation(sender, connectionValidateEventArgs);
            }
        }

        #endregion
        
        /// <summary>
        /// Determines whether this instance is valid.
        /// </summary>
        /// <returns></returns>
        public ValidationResult IsValid()
        {
            ValidationResult result = this.IsValidBucket();
            if (!result.IsValid)
            {
                this.cmbBucket_Validating(this.cmbBucket, null);
            }

            return result;
        }

        /// <summary>
        /// Determines whether [is valid bucket].
        /// </summary>
        /// <returns></returns>
        private ValidationResult IsValidBucket()
        {
            ValidationResult result = new ValidationResult(!this.cmbBucket.Text.Trim().IsNullOrEmpty());
            if (!result.IsValid)
            {
                result.Messages.Add("Bucket is required");
            }

            return result;
        }
    }
}
